﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace PhoneAppCheckSensors
{
    using Microsoft.Devices.Sensors;
    using Microsoft.Xna.Framework;

    public partial class PageCompass : PhoneApplicationPage
    {
        public PageCompass()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            if (mySensor == null)
            {
                mySensor = new Compass();
                mySensor.CurrentValueChanged += new EventHandler<SensorReadingEventArgs<CompassReading>>(mySensor_CurrentValueChanged);
            }

            base.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            if (buttonStop.IsEnabled)
            {
                mySensor.Stop();
                buttonStart.IsEnabled = true;
                buttonStop.IsEnabled = false;
                buttonDraw.IsEnabled = false;
            }
            base.OnNavigatedFrom(e);
        }

        private Compass mySensor;
        private void buttonStart_Click(object sender, RoutedEventArgs e)
        {
            gpTH.AdjustDrawArea();
            gpMH.AdjustDrawArea();
            gpMX.AdjustDrawArea();
            gpMY.AdjustDrawArea();
            gpMZ.AdjustDrawArea();

            buttonStart.IsEnabled = false;
            buttonStop.IsEnabled = true;
            mySensor.Start();
            buttonDraw.IsEnabled = true;
        }

        void mySensor_CurrentValueChanged(object sender, SensorReadingEventArgs<CompassReading> e)
        {
            Dispatcher.BeginInvoke(() =>
            {
                tbTimestamp.Text = e.SensorReading.Timestamp.ToString();
                double headingAccuracy = e.SensorReading.HeadingAccuracy;
                double magneticHeading = e.SensorReading.MagneticHeading;
                double trueHeading = e.SensorReading.TrueHeading;
                float magnetometerX = e.SensorReading.MagnetometerReading.X;
                float magnetometerY = e.SensorReading.MagnetometerReading.Y;
                float magnetometerZ = e.SensorReading.MagnetometerReading.Z;

                tbTH.Text = String.Format("{0:0.0000}", trueHeading);
                tbMH.Text = String.Format("{0:0.0000}", magneticHeading);
                tbHA.Text = String.Format("{0:0.0000}", headingAccuracy);

                tbMagX.Text = String.Format("{0:0.0000}", magnetometerX);
                tbMagY.Text = String.Format("{0:0.0000}", magnetometerY);
                tbMagZ.Text = String.Format("{0:0.0000}", magnetometerZ);

                gpTH.AddPoint(e.SensorReading.Timestamp, trueHeading);
                gpMH.AddPoint(e.SensorReading.Timestamp, magneticHeading);
                gpMX.AddPoint(e.SensorReading.Timestamp, magnetometerX);
                gpMY.AddPoint(e.SensorReading.Timestamp, magnetometerY);
                gpMZ.AddPoint(e.SensorReading.Timestamp, magnetometerZ);
            });
        }

        private void buttonStop_Click(object sender, RoutedEventArgs e)
        {
            buttonStop.IsEnabled = false;
            buttonStart.IsEnabled = true;
            mySensor.Stop();
        }

        private void buttonDraw_Click(object sender, RoutedEventArgs e)
        {
            gpTH.DrawGraph();
            gpMH.DrawGraph();
            gpMX.DrawGraph();
            gpMY.DrawGraph();
            gpMZ.DrawGraph();
        }

    }
}